$(document).ready(function() {
    let currentSlide = 0;
    const slides = $('.slide');
    const dots = $('.dot');
    const totalSlides = slides.length;

    // 检查是否存在轮播图容器，如果不存在则不执行轮播逻辑
    if ($('.slider-container').length === 0) {
        return;
    }

    function showSlide(index) {
        // 移除所有slide的类
        slides.removeClass('active prev next');
        dots.removeClass('active');

        // 设置当前slide
        $(slides[index]).addClass('active');

        // 设置前一个slide
        const prevIndex = (index - 1 + totalSlides) % totalSlides;
        $(slides[prevIndex]).addClass('prev');

        // 设置后一个slide
        const nextIndex = (index + 1) % totalSlides;
        $(slides[nextIndex]).addClass('next');

        // 更新指示点
        $(dots[index]).addClass('active');
    }

    function nextSlide() {
        currentSlide = (currentSlide + 1) % totalSlides;
        showSlide(currentSlide);
    }

    function prevSlide() {
        currentSlide = (currentSlide - 1 + totalSlides) % totalSlides;
        showSlide(currentSlide);
    }

    showSlide(currentSlide);

    let slideInterval = setInterval(nextSlide, 3000);

    $('.slider-container').hover(
        function() { clearInterval(slideInterval); },
        function() { slideInterval = setInterval(nextSlide, 3000); }
    );

    $('.next-arrow').click(function() {
        clearInterval(slideInterval);
        nextSlide();
        slideInterval = setInterval(nextSlide, 3000);
    });

    $('.prev-arrow').click(function() {
        clearInterval(slideInterval);
        prevSlide();
        slideInterval = setInterval(nextSlide, 3000);
    });

    $('.dot').click(function() {
        clearInterval(slideInterval);
        const slideIndex = $(this).data('slide');
        currentSlide = slideIndex;
        showSlide(currentSlide);
        slideInterval = setInterval(nextSlide, 3000);
    });
}); 